package com.arrogames.rgblabyrinth {
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import org.as3commons.logging.api.LOGGER_FACTORY;
	import org.as3commons.logging.setup.LogSetupLevel;
	import org.as3commons.logging.setup.RegExpSetup;
	import org.as3commons.logging.setup.target.TraceTarget;
	
	import starling.core.Starling;
	import starling.events.Event;
	
	[SWF(width="800", height="800", frameRate="60")]
	//[Frame(factoryClass="com.arrogames.rgblabyrinth.preloader.RGBPreloader")]
	public class Main extends Sprite {
				
		private var myStarling:Starling;
		private var myGame:RGBLabyrinth;
			
		public function Main() {
			super();
			
			if (stage == null) {
				addEventListener(flash.events.Event.ADDED_TO_STAGE, init );
			} else {
				init();
			}
			
		}
		
		private function init(event:flash.events.Event = null):void {
			var logTarget:TraceTarget = new TraceTarget();
			LOGGER_FACTORY.setup = new RegExpSetup()
                              .addTargetRule( /^com\.arrogames\./, logTarget, LogSetupLevel.DEBUG );
			
			// support autoOrients
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			if( stage.stageWidth && stage.stageHeight ) {
				removeEventListener(flash.events.Event.ADDED_TO_STAGE, init );
				myStarling = new Starling( RGBLabyrinth, stage );
				myStarling.antiAliasing = 0;
				myStarling.showStats = true;
				myStarling.start();
				myStarling.addEventListener(starling.events.Event.ROOT_CREATED, onRootCreated);
			}
		}
		
		private function onRootCreated(event:starling.events.Event, game:RGBLabyrinth):void {
			this.myGame = game;
			
			myStarling.removeEventListener(starling.events.Event.ROOT_CREATED, onRootCreated);
			
			AssetManager.instance.enqueueAll();
			AssetManager.instance.loadQueue(onProgress);
		}
		
		private function onProgress(ratio:Number):void {
			myGame.updateAssetProgress(ratio);
			
			if (ratio == 1) {
				myGame.loadingComplete();
			}
		}
	}
}